import path from 'node:path'
import vue from '@vitejs/plugin-vue'
import unoCSS from 'unocss/vite'
import { defineConfig } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import { watchAndRun } from 'vite-plugin-watch-and-run'

export default defineConfig(({ mode }) => {
  return {
    base: mode === 'dev' ? '' : '/options',
    plugins: [
      vue(),
      unoCSS(),
      createHtmlPlugin({
        template: 'options.html',
      }),
      watchAndRun([
        {
          watch: path.resolve('src/**/*.*'),
          run: 'pnpm build:dev',
          delay: 500,
        },
      ]),
    ],
    build: {
      outDir: 'dist/options',
      rollupOptions: {
        input: 'options.html',
        output: {
          entryFileNames: 'js/[name].js',
          chunkFileNames: 'js/[name].js',
          assetFileNames: 'css/[name].[ext]',
        },
      },
      chunkSizeWarningLimit: 2000,
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
    },
    server: {
      port: 3030,
    },
    define: {
      'process.env.NODE_ENV': null,
    },
  }
})
